/**
 * 初始化域名管理详情对话框
 */
var DomainInfoInfoDlg = {
    domainInfoInfoData : {}
};

/**
 * 清除数据
 */
DomainInfoInfoDlg.clearData = function() {
    this.domainInfoInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
DomainInfoInfoDlg.set = function(key, val) {
    this.domainInfoInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
DomainInfoInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
DomainInfoInfoDlg.close = function() {
    parent.layer.close(window.parent.DomainInfo.layerIndex);
}

/**
 * 收集数据
 */
DomainInfoInfoDlg.collectData = function() {
    let typeInfoMap = {
        "资源域名": "1",
        "落地页域名": "0"
    }
    let optype = this.get('optype')
    this
        .set('id')
        .set('domainName')
        .set('status')
        .set('type', typeInfoMap[this.get('type')])
    if(this.get('type') === '1') {
        if(optype === '1') {
            this.set('status', 2)
        }
    }
    DomainInfoInfoDlg.domainInfoInfoData['urlName'] = DomainInfoInfoDlg.domainInfoInfoData['urlName'] = DomainInfoInfoDlg.domainInfoInfoData['domainName'];
}

/**
 * 提交添加
 */
DomainInfoInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    console.log(this.domainInfoInfoData);
    this.domainInfoInfoData.domainName = this.domainInfoInfoData.domainName.trim();
    if(this.domainInfoInfoData.domainName === null || this.domainInfoInfoData.domainName.length == 0) {
        layer.msg("域名为空")
        return;
    }
    // 提交信息
    var ajax = new $ax(Feng.ctxPath + "/domainInfo/add", function(data){
        Feng.success("添加成功!");
        window.parent.DomainInfo.table.refresh();
        DomainInfoInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.domainInfoInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
DomainInfoInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    console.log("-----------------")
    console.log(DomainInfoInfoDlg.domainInfoInfoData)
    let idx = layer.load(0, {shade: false})
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/domainInfo/update", function(data){
        Feng.success("修改成功!");
        layer.closeAll()
        window.parent.DomainInfo.table.refresh();
        DomainInfoInfoDlg.close();
    },function(data){
        layer.closeAll()
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.domainInfoInfoData);
    ajax.start();
}

$(function() {
    let typeSel = document.getElementById('type')
    let statusSel = document.getElementById('status-row')
    if(statusSel !== null) {
        typeSel.onchange = function (e) {
            let curValue = e.target.value;
            if(curValue === "1") {
                statusSel.hidden = true
            } else {
                statusSel.hidden = false
            }
        }
    }

    layui.use(['form', 'laytpl'], function () {
        let form = layui.form
        let laytpl = layui.laytpl
        let list = []
        if($("#pre_status")[0].value === '1') {
            list = list.concat([
                {title: '失效', value: '0'},
                {title: '在用', value: '1'},
            ])
        } else if($("#pre_status")[0].value === '2')  {
            list = list.concat([
                {title: '失效', value: '0'},
                {title: '在用', value: '1'},
                {title: '备用', value: '2'}
            ])
        }
        let data = { list, value: $("#pre_status")[0].value}
        let target = $("#sel-status")[0];
        let tplStr = document.getElementById("status-sel-list").innerHTML
        laytpl(tplStr).render(data, function (html) {
            target.innerHTML = html
        })
        form.render();
    })
});
